package com.example.controller;

import com.example.dto.ReportListDto;
import com.example.service.OrderService;
import com.example.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.apache.logging.log4j.ThreadContext.isEmpty;

@RestController
@RequestMapping("/report")
public class ReportController {
    @Autowired
    private ReportService reportService;
    /**
     * 体检报告列表
     * @param userId
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<Map<String, Object>> reportList(@RequestParam("userId") String userId) {
        List<ReportListDto> orders = reportService.getOrdersWithHospitalNames(userId);
        Map<String, Object> map = new HashMap<>();
        if (orders != null && !orders.isEmpty()) {
            map.put("code", 200);
            map.put("msg", "success");
            map.put("orders", orders);
            map.put("total", orders.size());
            return ResponseEntity.status(HttpStatus.OK).body(map);
        } else {
            map.put("code", 400);
            map.put("msg", "fail");
            map.put("orders", null);
            map.put("total", 0);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(map);
        }
    }

    /**
     * 总检结论与体检报告详情
     * @param orderId
     * @return
     */
    @GetMapping("/details")
    public ResponseEntity<Map<String, Object>> reportDetails(@RequestParam("orderId") Integer orderId) {
        if(reportService.getCireportDetails(orderId) != null &&
            !reportService.getCireportDetails(orderId).getCidetailedreportList().isEmpty() &&
            !reportService.getCireportDetails(orderId).getOverallresultList().isEmpty()) {
            Map<String, Object> map = new HashMap<>();
            map.put("code", 200);
            map.put("msg", "success");
            map.put("report", reportService.getCireportDetails(orderId));
            return ResponseEntity.status(HttpStatus.OK).body(map);
        }else{
            Map<String, Object> map = new HashMap<>();
            map.put("code", 400);
            map.put("msg", "fail");
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(map);
        }

    }
}
